import java.util.Stack;

public class Test {
    //栈和队列的习题
    public boolean IsPopOrder (int[] pushV, int[] popV) {
        // write code here
        //栈的压入、弹出序列
        //https://www.nowcoder.com/practice/d77d11405cc7470d82554cb392585106?tpId=13&&tqId=11174&rp=1&ru=/activity/oj&qru=/ta/coding-interviews/question-ranking
        Stack<Integer> stack = new Stack<>();
        int j = 0;
        for (int i = 0; i < pushV.length; i++) {
            stack.push(pushV[i]);
            while(!stack.empty() && j < popV.length && stack.peek() == popV[j]) {
                stack.pop();
                j++;
            }
        }
        if (stack.empty()) {
            return true;
        }else {
            return false;
        }

    }
    private boolean isOperator(String ch) {
        if (ch.equals("+") || ch.equals("-") || ch.equals("*") || ch.equals("/")) {
            return true;
        }
        return false;
    }

    public int evalRPN(String[] tokens) {
        //逆波兰表达式求值
        //https://leetcode.cn/problems/evaluate-reverse-polish-notation/
        Stack<Integer> stack = new Stack<>();
        for (String s : tokens) {
            if (!isOperator(s)) {
                //不是运算符的话，就把字符串转换为数字，进行入栈
                int x = Integer.parseInt(s);//把s这个字符串转换为数字
                stack.push(x);
            } else {
                //是运算符的时候我们要出栈顶数据进行计算，
                //注意先出的数据放在运算符的右面
                int val2 = stack.pop();
                int val1 = stack.pop();
                switch (s) {
                    case "+":
                        stack.push(val1 + val2);
                        break;
                    case "-":
                        stack.push(val1 - val2);
                        break;
                    case "*":
                        stack.push(val1 * val2);
                        break;
                    case "/":
                        stack.push(val1 / val2);
                        break;
                }
            }
        }
        return stack.pop();
    }
    public boolean isValid(String s) {
        //括号匹配问题
        //使用栈
        //https://leetcode.cn/problems/valid-parentheses/
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char ch1 = s.charAt(i);
            if (ch1 == '(' || ch1 == '[' || ch1 == '{') {
                //左括号放进栈中
                stack.push(ch1);
            } else {
                //出现的是)}]
                if (stack.empty()) {
                    //栈如果为空，就是没有左括号，匹配不成功
                    return false;
                }
                //开始匹配
                char ch2 = stack.peek();
                if (ch1 == ')' && ch2 == '('
                        || ch1 == '}' && ch2 == '{'
                        || ch1 == ']' && ch2 == '[') {
                    //匹配成功的话，出栈顶数据
                    stack.pop();
                } else {
                    return false;
                }
            }
        }
        if (!stack.empty()) {
            // i 下标走完，看看栈是否为空，不为空就是没有匹配成功，有剩余
            return false;
        }
        return true;
    }
}
